package action;

import config.ApplicationConfig;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import server.HistoryServer;
import server.InitServer;
import server.LoginThreadServer;
import server.UserServer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.LinkedList;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2019-03-10 Sunday 00:34:56
 */
public class IndexServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
        request.setCharacterEncoding(pageConfig.get("encode"));
        response.setCharacterEncoding(pageConfig.get("encode"));

        ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
        int activeCount = threadGroup.activeCount();
        Thread[] threads = new Thread[activeCount];
        threadGroup.enumerate(threads);
        boolean flag = true;
        for (Thread thread : threads) {
            if (thread != null && thread.getName().equals("login")) {
                flag = false;
                break;
            }
        }
        if (flag) {
            LoginThreadServer loginThreadServer = new LoginThreadServer();
            loginThreadServer.open();
            Thread thread = new Thread(loginThreadServer);
            thread.setName("login");
            thread.start();
        }

        InitServer init = new InitServer();
        init.open();
        JSONObject result = init.initDatabase();
        init.close();
        HttpSession session = request.getSession();
        session.setAttribute("infoTitle", "数据库初始化");
        session.setAttribute("return", "index");
        if (result.has("username") && result.has("password")) {
            session.setAttribute("panelColor", "blue");
            String username = result.getString("username");
            session.setAttribute("infoContent", "数据库初始化成功<br><br>用户名:&nbsp;&nbsp;" + username + "<br><br>密码:&nbsp;&nbsp;" + result.getString("password"));

            UserServer userServer = new UserServer();
            userServer.open();
            JSONObject temp = userServer.searchUserByName(username);
            userServer.close();

            HistoryServer historyServer = new HistoryServer();
            historyServer.open();
            historyServer.record(temp.get("uuid").toString(), username + " 初始化数据库", request);
            historyServer.close();

            response.sendRedirect("information.jsp");
        } else {
            response.sendRedirect("index.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
